/*
 * Created on 
 *
 */
package socket;

import io.ReconnableAsyncSocketEventListener;
import io.stripframe.FrameProcessor;
import io.stripframe.FrameStripper;
import io.stripframe.HeaderTailStripper;

import java.nio.channels.SocketChannel;

import org.apache.log4j.Logger;


public class FunccodeClientEventListener implements  ReconnableAsyncSocketEventListener {
	private static Logger logger = Logger.getLogger(FunccodeClientEventListener.class);
	private FrameStripper stripper;
	private static char frameHeader = (char)2;
	private static char frameTail = (char)3;
	
	private class FunccodeClientFrameProcessor implements FrameProcessor {
		public void process(String s) {
			
		}
	}
	private FunccodeClient client;
	
	public FunccodeClientEventListener(FunccodeClient client) {
		this.client = client;
	}
	
	
	public void onDisconnect(SocketChannel channel) {
		this.onClose(channel);
	}
	public void onReceived(SocketChannel channel, String text) {
		this.onRead(channel, text);
	}

	public void onRegistered(int clientid, boolean isRegistered) {
		logger.info(" ClientId:" + clientid + " Registered:" + isRegistered);
	}

	@Override
	public void onClose(SocketChannel channel) {
		// TODO Auto-generated method stub
		logger.info("socket disconnected from "+channel.socket().getRemoteSocketAddress());
	}

	@Override
	public void onConnect(SocketChannel channel) {
		// TODO Auto-generated method stub
		logger.info(channel.socket().getRemoteSocketAddress()+" connect funccode server successfully");			
	    
		stripper = new HeaderTailStripper(frameHeader, frameTail);
		stripper.attachObserver(new FunccodeClientFrameProcessor());
	}



	@Override
	public void onRead(SocketChannel channel, String msg) {
		// TODO Auto-generated method stub
		logger.info("read from "+channel.socket().getRemoteSocketAddress()+" -- "+msg);
		if (stripper != null)
			stripper.putString(msg);
	}
	
}
